#ifndef MAPPED_RECOGNIZER_IMPL_H
#define MAPPED_RECOGNIZER_IMPL_H

#include <model/MappedRecognizer.h>

#include <vector>
#include <iostream>

template <class K>
MappedRecognizer<K>::MappedRecognizer()
{
}

template <class K>
void MappedRecognizer<K>::addModel(K key, const HMM &hmm) {
	models[key] = hmm;
}

template <class K>
HMM& MappedRecognizer<K>::getModel(K key) {
	return models[key];
}

template <class K>
HMM& MappedRecognizer<K>::operator [] (K key) {
	return models[key];
}

template <class K>
unsigned MappedRecognizer<K>::size() const {
	return models.size();
}

template <class K>
K MappedRecognizer<K>::recognize(const Observation &obs) const {
	if (models.size() == 0) {
		return 0;
	}
	double max = -1.0e+10;
        K maxkey("None");
        typename std::map<K, HMM>::const_iterator it = models.begin();
        for ( ; it != models.end(); it++) {
        	std::cout << "model \"" << it->first << "\" recognizing...\n";
		double prob = it->second.logProb(obs);
		if (prob <= 0 && prob > max) {
			max = prob;
			maxkey = it->first;
		}
		std::cout << it->first << ".logProb(observation) = " << prob << "\n";
	}
	return maxkey;
}

template <class K>
K MappedRecognizer<K>::recognize(const Observation &obs, std::vector<double> &probs) const {
	if (models.size() == 0) {
		return 0;
	}
	double max = -1.0e+10;
        K maxkey("None");
        typename std::map<K, HMM>::const_iterator it = models.begin();
        for ( ; it != models.end(); it++) {
        	std::cout << "model \"" << it->first << "\" recognizing...\n";
		double prob = it->second.logProb(obs);
		probs.push_back(prob);
		if (prob <= 0 && prob > max) {
			max = prob;
			maxkey = it->first;
		}
		std::cout << it->first << ".logProb(observation) = " << prob << "\n";
	}
	return maxkey;
}


template <class K>
void MappedRecognizer<K>::removeModel(K key) {
    models.erase(key);
}

#endif
